package util;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;

public class HtmlToTextService {

	private File dir; // directory path that stores all the html
	private String directory; // String equivalent of dir
	private String[] allFileNames; // all filenames in a dir
	private FileInputStream fis = null;
	private BufferedInputStream bis = null;
	private DataInputStream dis = null;
	private String jStr;
	private StringTokenizer tokenizer;
	private String token;

	public HtmlToTextService(String directory) {
		// TODO file should get from file names stored in Cassandra
		this.directory = directory;
		dir = new File(this.directory);
		allFileNames = dir.list();
		if (allFileNames == null) {
			System.out
					.println("Specified directory does not exist or is not a directory.");
			System.exit(0);
		}
	}

	@SuppressWarnings("deprecation")
	public void convertToText() throws IOException {
		String fileName;
		
		// TODO MKDIR don't rely on output dir being there
	    File targetDirFile = new File("output");
	    if (targetDirFile.exists()) {
	      FileUtils.forceDelete(targetDirFile);
	    }
		FileUtils.forceMkdir(targetDirFile);

	    
		try {
			for (int i = 0; i < allFileNames.length; i++) {
				fileName = "/" + allFileNames[i];
			    
				// setup write out
				FileWriter fstream = new FileWriter("output/" + fileName + ".txt");
				BufferedWriter out = new BufferedWriter(fstream);

				fis = new FileInputStream("html/" + fileName);

				// add BufferedInputStream for fast read
				bis = new BufferedInputStream(fis);
				dis = new DataInputStream(bis);

				// dis.available() returns 0 if the file does not have more lines
				while (dis.available() != 0) {
					jStr = Jsoup.parse(dis.readLine()).text();
					if (jStr != null) {
						tokenizer = new StringTokenizer(jStr);
						while (tokenizer.hasMoreTokens()) {
							token = tokenizer.nextToken();
							out.write(token);
							out.newLine();
						}
					}
				}
				out.close();
				fis.close();
				bis.close();
				dis.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
